<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <vxe-table 
      :data="AssetAuditConfigList"
      border
      :loading="loading"
      @selection-change="handleSelectionChange"
      stripe
      :row-id="row => row.id"
    >
      <template #toolbar>
        <vxe-toolbar :buttons="['export', 'import', 'search']" @search="getList"></vxe-toolbar>
      </template>

      <vxe-column field="configName" title="配置名称" align="center">
        <template #default="scope">
          <router-link :to="'/asset/auditConfigNodes/index/' + scope.row.id" class="link-type">
            <span>{{ scope.row.configName }}</span>
          </router-link>
        </template>
      </vxe-column>
      <vxe-column field="status" title="状态" align="center">
        <template #default="scope">
          <vxe-switch v-model="scope.row.status" :active-value="1" />
        </template>
      </vxe-column>
    </vxe-table>

    <div class="pagination-container">
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    
  </div>
</template>

<script>
import { listAssetAuditConfig, getAssetAuditConfig, delAssetAuditConfig, addAssetAuditConfig, updateAssetAuditConfig, exportAssetAuditConfig } from "@/api/asset/assetAuditConfig.js";

export default {
  name: "AssetAuditConfigManage",
  props: {
    configType: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      loading: true,
      exportLoading: false,
      ids: [],
      single: true,
      multiple: true,
      showSearch: true,
      total: 0,
      AssetAuditConfigList: [],
      title: "",
      open: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        configName: null,
        configDesc: null,
        configType: null,
        orderType: null,
        status: null,
        createUserId: null,
        updateUserId: null
      },
      form: {},
      columns: [
        { key: 1, label: "配置名称", visible: true },
        { key: 2, label: "配置描述", visible: true },
        { key: 3, label: "配置类型", visible: true },
        { key: 4, label: "请求类型", visible: true },
        { key: 5, label: "状态 0-未启用 1-启用", visible: true },
        { key: 9, label: "CREATE_USER_ID", visible: true },
        { key: 10, label: "UPDATE_USER_ID", visible: false },
      ],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      const {pageNum, pageSize} = this.queryParams;
      const query = {...this.queryParams, pageNum: undefined, pageSize: undefined};
      const pageReq = {page: pageNum - 1, size: pageSize};
      listAssetAuditConfig(query, pageReq, this.configType).then(response => {
        const { content, totalElements } = response;
        this.AssetAuditConfigList = content;
        this.total = totalElements;
        this.loading = false;
      });
    },
    cancel() {
      this.open = false;
      this.reset();
    },
    reset() {
      this.form = {
        id: null,
        configName: null,
        configDesc: null,
        configType: null,
        orderType: null,
        status: 0,
        createTime: null,
        updateTime: null,
        createUserId: null,
        updateUserId: null
      };
      this.resetForm("form");
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加【请填写功能名称】";
    },
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getAssetAuditConfig(id).then(response => {
        this.form = response;
        this.open = true;
        this.title = "修改【请填写功能名称】";
      });
    },
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAssetAuditConfig(this.form).then(() => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAssetAuditConfig(this.form).then(() => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除【请填写功能名称】编号为"' + ids + '"的数据项？').then(() => {
        return delAssetAuditConfig(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有【请填写功能名称】数据项？').then(() => {
        this.exportLoading = true;
        return exportAssetAuditConfig(queryParams);
      }).then(response => {
        this.$download.download(response);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>

<style lang="scss" scoped>
.app-container {
  display: flex;
  flex-direction: column;
  height: 86vh; 
  overflow: hidden; 
}

.vxe-table {
  flex: 1;
  overflow: auto;
}

.pagination-container {
  display: flex;
  justify-content: flex-end; 

}
</style>